#-*- coding:utf-8 -*-#
from xml.dom.minidom import Document

class XmlCreator:
    def __init__(self):
        self.logger = None


    def createXml(self, model, page=1, total_page=1):
        '''
        모델로 XML문서를 생성
        '''


        doc = Document()
        elResult = doc.createElement('result')
        doc.appendChild(elResult)
        elPage = doc.createElement('page')
        elPage.appendChild(doc.createTextNode(str(page)))
        elResult.appendChild(elPage)
        elTotal = doc.createElement('total')
        #total.appendChild(doc.createTextNode( str(len(account)) )
        elTotal.appendChild(doc.createTextNode( str(total_page) ))
        elResult.appendChild(elTotal)
        elRecord = doc.createElement('elRecord')
        elResult.appendChild(elRecord)
        elRows = doc.createElement("rows")

        for obj in model:
            row = doc.createElement('row')
            row.setAttribute('id', str(obj.id))
            field = obj.__dict__
            for f in field:
                #print f
                el = doc.createElement(f)
                el.appendChild(doc.createTextNode(unicode(getattr(obj, f))))
                row.appendChild(el)

            elRows.appendChild(row)

        elResult.appendChild(elRows)

        #print '-----------------------------------'
        xml = doc.toxml()
        #self.logger.info(xml)

        #print '-----------------------------------'

        return xml
